// src/chat/chat.controller.ts
import { Controller, Post, Body, Res, HttpStatus, Get, Query } from '@nestjs/common';
import { Response } from 'express'; // 或 fastify 的 Response
import { AliyunChatService } from './chat.service';
import { NotRequireAuth } from '../user/user.decorator'; // 校验 schema（用 zod/joi 等）

@Controller('system/chat')
export class ChatController {
  constructor(private readonly chatService: AliyunChatService) {}

  /**
   * 接收 GET 请求的 SSE 聊天接口
   * 从 Query 参数中获取 message 和 userId
   */
  @Get('stream')
  @NotRequireAuth()
  async streamChat(
    @Query('message') message: string,
    @Query('sessionId') sessionId: string, // GET 参数默认是字符串，需转换为数字
    @Res() res: Response,
  ) {
    try {
      // 验证参数并转换 userId 为数字
      if (!message || !sessionId) {
        res.status(HttpStatus.BAD_REQUEST).json({
          statusCode: HttpStatus.BAD_REQUEST,
          message: 'message（字符串）和 userId（字符串）为必填参数',
        });
        return;
      }

      // 调用服务层处理 SSE 逻辑
      await this.chatService.sendSSEChatMessage(message, sessionId, res);
    } catch (error) {
      res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
        statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
        message: error.message || '服务器内部错误',
      });
    }
  }
}
